from rest_framework import serializers

from backend.po.supplier import Supplier
from backend.po.buy_order import BuyOrder
from backend.po.take_delivery import TakeDelivery
from backend.po.recv_invoice import RecvInvoice
from backend.po.payment4buy import Payment4BuyOrder


class CommonSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    remark = serializers.CharField(required=False, max_length=255)


class SupplierObjectSerializer(CommonSerializer):
    name = serializers.CharField(max_length=90, source='supplier_name')
    contact_name = serializers.CharField(max_length=90)
    contact_no = serializers.CharField(max_length=32)

    def create(self, validated_data):
        validated_data['supplier_name'] = validated_data['name']
        return Supplier(**validated_data)


class BuyOrderObjectSerializer(CommonSerializer):
    buy_date_time = serializers.DateTimeField()
    digest = serializers.CharField(max_length=128)
    amount = serializers.DecimalField(max_digits=24, decimal_places=6)
    price = serializers.DecimalField(max_digits=24, decimal_places=6)
    unit = serializers.CharField(max_length=32)
    is_payed = serializers.IntegerField()
    is_recv_invoice = serializers.IntegerField()
    is_take_delivery = serializers.IntegerField()
    supplier = SupplierObjectSerializer(required=False, read_only=True)

    def create(self, validated_data):
        supplier = None
        validated_data['is_payed'] = bool(validated_data['is_payed'])
        validated_data['is_recv_invoice'] = bool(validated_data['is_recv_invoice'])
        validated_data['is_take_delivery'] = bool(validated_data['is_take_delivery'])
        supplier_data = validated_data.get('supplier_detail')
        if supplier_data:
            supplier = SupplierObjectSerializer().create(supplier_data)
        return BuyOrder(**validated_data, supplier=supplier)


class ForeignBuyOrderObjectSerializer(CommonSerializer):
    object_class = None
    buy_order = BuyOrderObjectSerializer(required=False, read_only=True)

    def create(self, validated_data):
        assert self.object_class is not None, f'object_class can not be none'
        buy_order = None
        order_data = validated_data.get('buy_order_detail')
        if order_data:
            buy_order = BuyOrderObjectSerializer().create(order_data)
        return self.object_class(**validated_data, buy_order=buy_order)


class DeliveryObjectSerializer(ForeignBuyOrderObjectSerializer):
    object_class = TakeDelivery

    take_delivery_date_time = serializers.DateTimeField()
    take_delivery_no = serializers.CharField(max_length=255)


class InvoiceObjectSerializer(ForeignBuyOrderObjectSerializer):
    object_class = RecvInvoice

    recv_invoice_date_time = serializers.DateTimeField()
    recv_invoice_no = serializers.CharField(max_length=255)
    recv_invoice_file_url = serializers.CharField(max_length=255)


class BuyOrderPaymentObjectSerializer(ForeignBuyOrderObjectSerializer):
    object_class = Payment4BuyOrder

    pay_date_time = serializers.DateTimeField()
    pay_type = serializers.IntegerField()
    amount = serializers.DecimalField(max_digits=24, decimal_places=6)
    pay_flow_seq = serializers.CharField(max_length=255)
